package com.dianrui.agent.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.activity.CarMonitoringActivity;
import com.dianrui.agent.activity.ChooseNetWorkActivity;
import com.dianrui.agent.activity.DeviceListActivity;
import com.dianrui.agent.activity.FinaceDetailActivity;
import com.dianrui.agent.activity.LoginActivity;
import com.dianrui.agent.activity.MaintenanceOrderListActivity;
import com.dianrui.agent.activity.ManagerActivity;
import com.dianrui.agent.activity.OperationAreaActivity;
import com.dianrui.agent.activity.PlanningActivity;
import com.dianrui.agent.activity.PlatformWebViewActivity;
import com.dianrui.agent.activity.PowerExchangeActivity;
import com.dianrui.agent.activity.ShowNetworkMapActivity;
import com.dianrui.agent.activity.ViolationOrderListActivity;
import com.dianrui.agent.event.ChooseNetwork;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;


public class FragmentMainWork extends BaseFragment {

    private String fixeid;
    private String url;

    @BindView(R.id.right_txt)
    TextView rightTxt;
    @BindView(R.id.home_work_date)
    TextView homeWorkDate;
    @BindView(R.id.earnings)
    TextView earnings;
    @BindView(R.id.order_count)
    TextView orderCount;
    @BindView(R.id.vehicle_count)
    TextView vehicleCount;
    @BindView(R.id.vehicle_start)
    TextView vehicleStart;
    @BindView(R.id.home_name)
    TextView netWorkname;
    @BindView(R.id.home_city)
    TextView netWorkcity;
    @BindView(R.id.charging_count)
    TextView chargingCount;
    @BindView(R.id.move_count)
    TextView moveCount;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.maintain)
    TextView maintain;
    @BindView(R.id.violation)
    TextView violation;



    @Override
    public int getContentViewId() {
        return R.layout.fragment_main_worktable;
    }

    @Override
    public void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 EEEE");
        Date date = new Date(System.currentTimeMillis());
        homeWorkDate.setText(simpleDateFormat.format(date));
        requestWorkInfo();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                requestWorkInfo();
            }
        });

    }


    //获取工作台设备界面信息
    public void requestWorkInfo() {
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl() + Url.DEVICE_IFNO_MAIN, "", new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if ("1000".equals(jsonObject.optString("status"))) {
                            refreshLayout.finishRefresh();
                            spUtils.put("area", result);
                            fixeid = jsonObject.optJSONObject("data").optString("fixed_id");
                            vehicleCount.setText(jsonObject.optJSONObject("data").optString("vehicle_count"));
                            SharedUtil.setAreaid(jsonObject.optJSONObject("data").optString("area_id"));
                            netWorkname.setText("区域:" + jsonObject.optJSONObject("data").optString("area_name"));
                            netWorkcity.setText(jsonObject.optJSONObject("data").optString("remark"));
                            vehicleStart.setText(jsonObject.optJSONObject("data").optString("vehicle_start"));
                            earnings.setText(jsonObject.optJSONObject("data").optString("earnings"));
                            orderCount.setText(jsonObject.optJSONObject("data").optString("order_count"));
                            maintain.setText(jsonObject.optJSONObject("data").optString("maintain"));
                            violation.setText(jsonObject.optJSONObject("data").optString("violation"));
                            chargingCount.setText(jsonObject.optJSONObject("data").optString("charging_count"));
                            moveCount.setText(jsonObject.optJSONObject("data").optString("move_count"));
                            if (jsonObject.optJSONObject("data").optInt("is_view") == 0) {
                                rightTxt.setVisibility(View.GONE);
                            }else {
                                rightTxt.setVisibility(View.VISIBLE);
                            }
                            url= jsonObject.optJSONObject("data").optString("jump_url");


                        } else if ("1001".equals(jsonObject.optString("status"))) {
                            ToastUtil.showToast(jsonObject.optString("message"));
                        }else if ("1002".equals(jsonObject.optString("status"))) {
                            spUtils.clear();
                            ToastUtil.showToast(jsonObject.optString("message"));
                            JumpActivitys(LoginActivity.class);
                            getActivity().finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void FailedRequest(String result) {
                refreshLayout.finishRefresh();
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ChooseNetwork chooseNetwork) {
        requestWorkInfo();
    }


    @OnClick({R.id.layout1, R.id.layout2, R.id.layout3,R.id.income_layout, R.id.todayorder_layout, R.id.choose_networks, R.id.manager, R.id.right_txt,R.id.maintain_layout, R.id.home_city, R.id.violation_layout, R.id.all_device, R.id.all_device_online, R.id.powerexchange_layout, R.id.carpark_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout1:
                startActivity(new Intent(getActivity(), DeviceListActivity.class).putExtra("mode", "1"));

                break;
            case R.id.layout2:
                JumpActivitys(ShowNetworkMapActivity.class);

                break;
            case R.id.layout3:
                JumpActivitys(CarMonitoringActivity.class);

                break;


            case R.id.choose_networks:
                JumpActivitys(ChooseNetWorkActivity.class);
                break;
            case R.id.manager:
                JumpActivitys(ManagerActivity.class);
                break;
            case R.id.maintain_layout:
                JumpActivitys(MaintenanceOrderListActivity.class);
                break;

            case R.id.violation_layout:
                JumpActivitys(ViolationOrderListActivity.class);
                break;
            case R.id.all_device:
                JumpActivitys(DeviceListActivity.class);
                break;

            case R.id.all_device_online:
                JumpActivitys(DeviceListActivity.class);
                break;

            case R.id.home_city:
                JumpActivitys(OperationAreaActivity.class);
                break;

            case R.id.powerexchange_layout://换电次数
                JumpActivitys(PowerExchangeActivity.class);
                break;

            case R.id.carpark_layout://调度次数
                JumpActivitys(PlanningActivity.class);
                break;

            case R.id.income_layout:
                JumpActivitys(FinaceDetailActivity.class);
                break;

            case R.id.todayorder_layout:
                JumpActivitys(FinaceDetailActivity.class);
                break;
            case R.id.right_txt:
                Intent intent=new Intent(getActivity(), PlatformWebViewActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("url",url);
                intent.putExtras(bundle);
                startActivity(intent);
                break;

            default:
                break;
        }
    }
}
